package com.mazaiting.redeye.module.me.dd

import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityEvent
import com.mazaiting.accessibility.Config
import com.mazaiting.accessibility.interfaces.IAccessibilityService
import com.mazaiting.accessibility.util.AccessibilityUtil
import com.mazaiting.log.L

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-25
 * @description 钉钉服务
 */
class DingDingService : IAccessibilityService {

    override fun getPackageNames(): Array<String> = arrayOf(Config.PACKAGE_DINGDING)

    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        // 标识为钉钉页面
        if (Config.PACKAGE_DINGDING == event.packageName) {
            when (event.eventType) {
                // 窗口状态改变
                AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {
                    L.d("状态改变: $event")
                }
                // 窗口内容改变
                AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED -> {
//                    L.d("界面改变: " + event.className)

                    if ("android.widget.ListView" == event.className) {
                        // 开启工作通知页面
                        val workNotification = AccessibilityUtil.clickText("工作通知:乾元大通科技集团有限公司")
                        L.d("workNotification: $workNotification")
                        if (!workNotification) {
                            // 开启打卡页面
                            val punch = AccessibilityUtil.clickText("考勤打卡", false)
                            L.d("punch: $punch")
                        }
                    }
//                    if (1 == index) {
//                        val text =
//                            rootInActiveWindow.findAccessibilityNodeInfosByText("下班打卡")
//                        L.d("event.className: ${event.className}, event.eventType: ${event.eventType}, text: $text")
//                    }
//                    if ("android.webkit.WebView" == event.className) {
//                        val upPunch = AccessibilityOperator.INSTANCE.clickText("下班时间", false)
//                        L.d("upPunch: $upPunch")
//                    }
                }

            }
        }
    }

}